﻿Imports Telerik.Web.UI
Imports System.Data.SqlClient
Imports System.Data

Partial Class Purchase_PurchaseRequestDtl
    Inherits System.Web.UI.Page

    Dim _CommandName As String
    Dim _Command As String
    Dim Grid As RadGrid
    Dim PurchaseRequestHdrObj As New DBPurchaseRequestHdrSP
    Dim PurchaseRequestHdrDS As New Data.DataSet
    Dim PurchaseRequestDtlObj As New DBPurchaseRequestDtlSP
    Dim PurchaseRequestDtlDS As New Data.DataSet
    Dim ItemObj As New DBItemSP
    Dim ItemDataSet As New DataSet
    Dim UnitObj As New DBUnitSP
    Dim UnitDataSet As New DataSet

    Protected Sub Page_PreInit(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreInit
        Me.Theme = GetGlobalResourceObject("SystemResource", "Theme")
    End Sub

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not Page.IsPostBack Then
            Dim _lblPageTitle As Label
            _lblPageTitle = Me.Master.FindControl("lblPageTitle")
            _lblPageTitle.Text = GetLocalResourceObject("PageTitle")

            hdnPurReqID.Value = Request.QueryString("ID").ToString

            If hdnPurReqID.Value <> "" And hdnPurReqID.Value <> "0" And hdnPurReqID.Value <> "-1" Then
                FillPurchaseRequestDtl()
            Else
                hdnPurReqID.Value = "-1"
                rdpPurReqDate.DbSelectedDate = DateTime.Now()
                rbtnDelete.Visible = False
            End If
            btnCancel.Attributes.Add("onClick", "javascript:history.back(); return false;")
        End If
    End Sub

    Protected Sub rgPurchaseRequestDtl_ItemCommand(ByVal sender As Object, ByVal e As Telerik.Web.UI.GridCommandEventArgs) Handles rgPurchaseRequestDtl.ItemCommand
        _CommandName = e.CommandName
    End Sub


    Sub SaveControls()
        hdnPurReqID.Value = PurchaseRequestHdrObj.QueryDatabase(DBParentStoredProcedureClass.SqlQueryType.ExecuteScalar, "SavePurchaseRequestHdr", _
                                                      hdnPurReqID.Value, rtbPurReqNo.Text, rdpPurReqDate.DbSelectedDate, rcbPurReqType.SelectedValue, rcbCostCenter.SelectedValue, _
                                                      rcbMainCostCenter.SelectedValue, rtbRemarks.Text, , Session("UserName").ToString(), Now)
        SaveDtl()
    End Sub

    Protected Sub SaveDtl()

        PurchaseRequestDtlObj.QueryDatabase(DBParentStoredProcedureClass.SqlQueryType.ExecuteNonQuery, "DeletePurchaseRequestDtlByPurchaseRequestID", , hdnPurReqID.Value)

        For counter = 0 To rgPurchaseRequestDtl.Items.Count - 1
            PurchaseRequestDtlObj.QueryDatabase(DBParentStoredProcedureClass.SqlQueryType.ExecuteNonQuery, "SavePurchaseRequestDtl", rgPurchaseRequestDtl.Items(counter)("ID").Text, _
            hdnPurReqID.Value, _
            IIf(rgPurchaseRequestDtl.Items(counter)("ProgramNo").Text = "&nbsp;", "", rgPurchaseRequestDtl.Items(counter)("ProgramNo").Text), _
            IIf(rgPurchaseRequestDtl.Items(counter)("ProjectNo").Text = "&nbsp;", "", rgPurchaseRequestDtl.Items(counter)("ProjectNo").Text), _
             IIf(rgPurchaseRequestDtl.Items(counter)("Item").Text = "&nbsp;", "", rgPurchaseRequestDtl.Items(counter)("Item").Text), _
             IIf(rgPurchaseRequestDtl.Items(counter)("Unit").Text = "&nbsp;", "", rgPurchaseRequestDtl.Items(counter)("Unit").Text), _
             IIf(rgPurchaseRequestDtl.Items(counter)("UnitPrice").Text = "&nbsp;", "", rgPurchaseRequestDtl.Items(counter)("UnitPrice").Text), _
             IIf(rgPurchaseRequestDtl.Items(counter)("Qty").Text = "&nbsp;", "", rgPurchaseRequestDtl.Items(counter)("Qty").Text), _
             IIf(rgPurchaseRequestDtl.Items(counter)("PurchaseReason").Text = "&nbsp;", "", rgPurchaseRequestDtl.Items(counter)("PurchaseReason").Text), _
             IIf(rgPurchaseRequestDtl.Items(counter)("Remarks").Text = "&nbsp;", "", rgPurchaseRequestDtl.Items(counter)("Remarks").Text))
        Next
    End Sub

    Protected Sub FillPurchaseRequestDtl()
        PurchaseRequestHdrDS = PurchaseRequestHdrObj.QueryDatabase(DBParentStoredProcedureClass.SqlQueryType.FillDataset, "RtvPurchaseRequestHdrByID", hdnPurReqID.Value)
        If Not PurchaseRequestHdrDS Is Nothing Then
            If PurchaseRequestHdrDS.Tables.Count > 0 Then
                If PurchaseRequestHdrDS.Tables(0).Rows.Count > 0 Then

                    rtbPurReqNo.Text = PurchaseRequestHdrDS.Tables(0).Rows(0).Item("PurReqNo").ToString()
                    rdpPurReqDate.DbSelectedDate = PurchaseRequestHdrDS.Tables(0).Rows(0).Item("PurReqDate").ToString()
                    rcbPurReqType.DataBind()
                    rcbPurReqType.SelectedValue = PurchaseRequestHdrDS.Tables(0).Rows(0).Item("PurReqType").ToString()
                    rcbCostCenter.DataBind()
                    rcbCostCenter.SelectedValue = PurchaseRequestHdrDS.Tables(0).Rows(0).Item("CostCenter").ToString()
                    rcbMainCostCenter.DataBind()
                    rcbMainCostCenter.SelectedValue = PurchaseRequestHdrDS.Tables(0).Rows(0).Item("MainCostCenter").ToString()
                    rtbRemarks.Text = PurchaseRequestHdrDS.Tables(0).Rows(0).Item("Remarks").ToString()

                    'If hdnRecordStatus.Value = "2" Then 'Voucher is exported so fields disabled
                    '    rtbYear.Enabled = False
                    'End If
                End If
            End If
        End If
    End Sub


    Protected Sub btnSave_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSave.Click
        Try
                SaveControls()
                RadAjaxManager1.ResponseScripts.Add("radalert('Information Saved Successfully !', 270, 100);")
        Catch ex As Exception
            Throw ex
        End Try
    End Sub

    Protected Sub btnCancel_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnCancel.Click

    End Sub

    Protected Sub rbtnDelete_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles rbtnDelete.Click
        DeleteRecord()
    End Sub
    Sub DeleteRecord()

        Try
            PurchaseRequestDtlObj.QueryDatabase(DBParentStoredProcedureClass.SqlQueryType.ExecuteNonQuery, "DeletePurchaseRequestDtlByPurchaseRequestID", , hdnPurReqID.Value)
            PurchaseRequestHdrObj.QueryDatabase(DBParentStoredProcedureClass.SqlQueryType.ExecuteNonQuery, "DeletePurchaseRequestHdr", hdnPurReqID.Value)
            Response.Redirect("~/Purchase/PurchaseRequestHdr.aspx")

        Catch ex As Exception
            Response.Write(ex.Message)
        Finally

        End Try
    End Sub

    Protected Sub rgPurchaseRequestDtl_ItemDataBound(ByVal sender As Object, ByVal e As Telerik.Web.UI.GridItemEventArgs) Handles rgPurchaseRequestDtl.ItemDataBound
        If (TypeOf e.Item Is GridEditFormItem AndAlso e.Item.IsInEditMode) And _CommandName <> "InitInsert" Then
            'If (TypeOf e.Item Is GridEditFormItem AndAlso e.Item.IsInEditMode) Then

            Dim editFormItem As GridEditFormItem = CType(e.Item, GridEditFormItem)
            Dim parentItem As GridDataItem = editFormItem.ParentItem

            Dim box As RadComboBox = CType(editFormItem.FindControl("WUCItem").FindControl("rcbv"), RadComboBox)
            box.SelectedValue = parentItem("Item").Text
            Dim box1 As RadComboBox = CType(editFormItem.FindControl("WUCItem").FindControl("rcbd"), RadComboBox)
            box1.SelectedValue = parentItem("Item").Text

            Dim box2 As RadComboBox = CType(editFormItem.FindControl("WUCUnit").FindControl("rcbv"), RadComboBox)
            box2.SelectedValue = parentItem("Unit").Text
            Dim box3 As RadComboBox = CType(editFormItem.FindControl("WUCUnit").FindControl("rcbd"), RadComboBox)
            box3.SelectedValue = parentItem("Unit").Text

        End If

    End Sub

    Protected Sub WUCItem_SelectedIndexChanged(ByVal sender As Object, ByVal e As RadComboBoxSelectedIndexChangedEventArgs)
        Dim activeEditItem As Integer = 0
        For i As Integer = 0 To rgPurchaseRequestDtl.Items.Count - 1
            If rgPurchaseRequestDtl.Items(i).Edit Then
                activeEditItem = i
                Exit For
            End If
        Next

        Dim editFormItem As GridEditFormItem = DirectCast(rgPurchaseRequestDtl.MasterTableView.GetItems(GridItemType.EditFormItem)(activeEditItem), GridEditFormItem)
        odsDynamicUnit.SelectParameters("UnitID").DefaultValue = e.Value
        CType(editFormItem.FindControl("WUCUnit").FindControl("rcbv"), RadComboBox).DataBind()
    End Sub


End Class
